import os
import random

from django.shortcuts import render

# Create your views here.
from rest_framework.views import APIView
from django.utils.safestring import mark_safe
from rest_framework.response import Response

from shareAPI2020TB import settings
from shareAPI2020TB.settings import BASE_DIR
from tbitem import models

from tbitem.util.get_0_diy import setp0
from tbitem.util.get_2_keyWord import step2
from tbitem.util.get_3_tbContent import tbmain, jdmain
from tbitem.util.get_tbContent_and_makeTemplete import step5, ContentFactory, downloadImg, createWord
from tbitem.util.reMatch import storeJsonFile_To_Db
from tbitem.util.测试发帖 import stepTest


class APIResponse(Response):

    def __init__(self, data=0, msg='success', code=200, http_status=None, headers=None, exception=False, **kwargs):
        # 将外界传入的数据状态码，状态信息以及其他所有额外储存在kwargs中的信息，都格式化成data信息
        data = {
            'code': code,  # 前端用来判断的状态码
            'data': data,  # 数据状态码
            'msg': msg  # 解释内容
        }
        if kwargs:
            data.update(kwargs)
        super().__init__(data=data, status=http_status, headers=headers, exception=exception)



from tbitem.util.TimeRun import startRun, endRun, collectDBData, postJdjog
import threading
import logging
server_logger = logging.getLogger("server")
class Link(APIView):
    authentication_classes = []
    def get(self, request, *args, **kwargs):
        bokeeconfig_QuerySetList = models.QiBoAccount.objects.all()
        print(bokeeconfig_QuerySetList)
        for i in  bokeeconfig_QuerySetList:
            print(type(i))
            abc = i
            print(type(abc))
            i.title ='34324'
            print(i.title)
        return APIResponse(0,msg=f'运行成功')


class StartRun(APIView):  #开启任务
    authentication_classes = []
    def get(self, request, *args, **kwargs):
        startRun()
        return APIResponse(4040)



class StartDiyInitRun(APIView):
    authentication_classes = []
    def get(self, request, *args, **kwargs):
        # server_logger.error("开始启动调度程序")
        setp0()
        # get_1_links.step1()
        # get_2_keyWord.step2()
        # get_3_tbContent.step3()
        # get_4_postQb.step4()
        return APIResponse(0, msg="Ok")


class StepTest(APIView):
    authentication_classes = []
    def get(self, request, *args, **kwargs):
        step2()
        return APIResponse(0,msg='123')


#显示id下的所有图片和评论
class ID(APIView):
    authentication_classes = []
    def get(self, request, *args, **kwargs):
        tbid = kwargs.get('id')
        # 根据id ，获得对应的素材
        step5(tbid)  # 负责把bbs的内容遍历淘宝id，抓取淘宝评论到数据库
        TBimg_Query = models.TbIMG.objects.filter(tbimg_Index__tbid=tbid).all()
        TBComment_Query = models.TbComment.objects.filter(tbComment_Index__tbid=tbid).all()
        content = ContentFactory(TBimg_Query, TBComment_Query,tbid)
        # downloadImg(tbid, 'http://img.alicdn.com/imgextra/i1/0/O1CN01fhHiRQ1mLByGKPk8X_!!0-rate.jpg_400x400.jpg')

        # createWord(tbid)
        #保存图片到本地


        return Response('123123')





class Vip(APIView):  #开启任务
    authentication_classes = []
    def get(self, request, *args, **kwargs):
        # server_logger.error("开始启动调度程序")
        dbDatas = collectDBData()  #数据库的基本信息

        return APIResponse(0, msg=dbDatas)

class Insertdb(APIView):
    authentication_classes = []

    def get(self, request, *args, **kwargs):
        storeJsonFile_To_Db()
        return APIResponse(0,msg='插入成功')